package com.wintrue.ffxs.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wintrue.ffxs.R;
import com.wintrue.ffxs.base.BaseActivity;
import com.wintrue.ffxs.ui.adapter.GuideViewPagerAdapter;
import com.wintrue.ffxs.ui.home.MainActivity;
import com.wintrue.ffxs.utils.ActivityUtil;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @des: 引导页
 * @author:th
 * @email:342592622@qq.com
 * @date: 2017/5/19 0019 上午 9:36
 */
public class GuidanceActivity extends BaseActivity {
    @Bind(R.id.viewpager)
    ViewPager mViewpager;
    @Bind(R.id.m_ProgressView)
    TextView mProgressView;
    @Bind(R.id.page0)
    ImageView pointImage0;
    @Bind(R.id.page1)
    ImageView pointImage1;
    @Bind(R.id.page2)
    ImageView pointImage2;
    @Bind(R.id.page3)
    ImageView pointImage3;

    private GuideViewPagerAdapter mAdapter;
    private ArrayList<View> mViews;
    private RelativeLayout mView1, mView2, mView3, mView4;
    private WebView mWebView1,mWebView2,mWebView3,mWebView4;
    private boolean mScrolled = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guidance);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        mProgressView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toMain();
            }
        });
        LayoutInflater mLi = LayoutInflater.from(this);
        mView1 = (RelativeLayout)mLi.inflate(R.layout.view_guide1, null);
        mView2 = (RelativeLayout)mLi.inflate(R.layout.view_guide2, null);
        mView3 = (RelativeLayout)mLi.inflate(R.layout.view_guide3, null);
        mView4 = (RelativeLayout)mLi.inflate(R.layout.view_guide4, null);
        mWebView1 = (WebView) mView1.findViewById(R.id.web_view);
        mWebView1.setBackgroundColor(Color.parseColor("#00000000"));
        String path1 = "file:///android_asset/bg_guide1.png";
        String content1 = "<html><head></head><body style=\"margin:0;padding:0;width:100%;height:100%;\"><img src=\""+path1+"\" style=\"width:100%;height:100%;\"/></body></html>";
        mWebView1.loadDataWithBaseURL(null, content1, "text/html", "utf-8", null);

        mWebView2 = (WebView) mView2.findViewById(R.id.web_view);
        mWebView2.setBackgroundColor(Color.parseColor("#00000000"));
        String path2 = "file:///android_asset/bg_guide2.png";
        String content2 = "<html><head></head><body style=\"margin:0;padding:0;width:100%;height:100%;\"><img src=\""+path2+"\" style=\"width:100%;height:100%;\"/></body></head></html>";
        mWebView2.loadDataWithBaseURL(null, content2, "text/html", "utf-8", null);

        mWebView3 = (WebView) mView3.findViewById(R.id.web_view);
        mWebView3.setBackgroundColor(Color.parseColor("#00000000"));
        String path3 = "file:///android_asset/bg_guide3.png";
        String content3 = "<html><head></head><body style=\"margin:0;padding:0;width:100%;height:100%;\"><img src=\""+path3+"\" style=\"width:100%;height:100%;\"/></body></html>";
        mWebView3.loadDataWithBaseURL(null, content3, "text/html", "utf-8", null);

        mWebView4 = (WebView) mView4.findViewById(R.id.web_view);
        mWebView4.setBackgroundColor(Color.parseColor("#00000000"));
        String path4 = "file:///android_asset/bg_guide4.png";
        String content4 = "<html><head></head><body style=\"margin:0;padding:0;width:100%;height:100%;\"><img src=\""+path4+"\" style=\"width:100%;height:100%;\"/></body></html>";
        mWebView4.loadDataWithBaseURL(null, content4, "text/html", "utf-8", null);

        mView4.findViewById(R.id.btn_receive).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toMain();
            }
        });
    }

    /**
     * 初始化数据
     */
    private void initData() {
        mViewpager = (ViewPager) findViewById(R.id.viewpager);
        mViews = new ArrayList<>();
        mAdapter = new GuideViewPagerAdapter(mViews);
        mViewpager.setAdapter(mAdapter);
        mViewpager.setOnPageChangeListener(new MyOnPageChangeListener());
        mViews.add(mView1);
        mViews.add(mView2);
        mViews.add(mView3);
        mViews.add(mView4);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    pointImage0.setImageResource(R.drawable.page_indicator_focused);
                    pointImage1.setImageResource(R.drawable.page_indicator_unfocused);
                    pointImage2.setImageResource(R.drawable.page_indicator_unfocused);
                    pointImage3.setImageResource(R.drawable.page_indicator_unfocused);
                    break;
                case 1:
                    pointImage0.setImageResource(R.drawable.page_indicator_unfocused);
                    pointImage1.setImageResource(R.drawable.page_indicator_focused);
                    pointImage2.setImageResource(R.drawable.page_indicator_unfocused);
                    pointImage3.setImageResource(R.drawable.page_indicator_unfocused);
                    break;
                case 2:
                    pointImage0.setImageResource(R.drawable.page_indicator_unfocused);
                    pointImage1.setImageResource(R.drawable.page_indicator_unfocused);
                    pointImage2.setImageResource(R.drawable.page_indicator_focused);
                    pointImage3.setImageResource(R.drawable.page_indicator_unfocused);
                    break;
                case 3:
                    pointImage0.setImageResource(R.drawable.page_indicator_unfocused);
                    pointImage1.setImageResource(R.drawable.page_indicator_unfocused);
                    pointImage2.setImageResource(R.drawable.page_indicator_unfocused);
                    pointImage3.setImageResource(R.drawable.page_indicator_focused);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            switch (state) {
                case ViewPager.SCROLL_STATE_DRAGGING:
                    mScrolled = false;
                    break;
                case ViewPager.SCROLL_STATE_SETTLING:
                    mScrolled = true;
                    break;
                case ViewPager.SCROLL_STATE_IDLE:
                    if (mViewpager.getCurrentItem() == mViewpager.getAdapter().getCount() - 1 && !mScrolled) {
                        toMain();
                    }
                    mScrolled = true;
                    break;
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }
    }

    /**
     * 相应按钮点击事件
     */
    private void toMain() {
        ActivityUtil.next(this, MainActivity.class, R.anim.fade_in,
                R.anim.fade_out, true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mView1.removeView(mWebView1);
        mWebView1.removeAllViews();
        mWebView1.destroy();
        mWebView1 = null;

        mView2.removeView(mWebView2);
        mWebView2.removeAllViews();
        mWebView2.destroy();
        mWebView2 = null;

        mView3.removeView(mWebView3);
        mWebView3.removeAllViews();
        mWebView3.destroy();
        mWebView3 = null;

        mView4.removeView(mWebView4);
        mWebView4.removeAllViews();
        mWebView4.destroy();
        mWebView4 = null;
    }
}
